package com.yc.mvplib.permission;

import android.app.Activity;
import android.content.Context;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yc.mvplib.utlis.ActivityStackUtil;

import java.util.List;

import static com.yc.mvplib.FastConfig.REQUEST_CODE_SETTING;

/**
 * 描述：6.0运行时权限
 * 创建者：  Churck
 * 创建时间：2017/12/7 15:53
 * 修改者： Churck
 * 修改时间：2017/12/7 15:53
 * 版 本：v1.0
 */

public class PermissionRequest {

    private PermissionCallback mCallback;
    public PermissionRequest(PermissionCallback callback) {
        this.mCallback = callback;
    }
    public PermissionRequest() {
    }

    public void setmCallback(PermissionCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void request(Context mContext, String[]...permissionsArray) {
        AndPermission.with(mContext)
                .requestCode(REQUEST_CODE_SETTING)
                .permission(permissionsArray)
                .callback(this)
                .start();
    }

    @PermissionYes(REQUEST_CODE_SETTING)
    public void getPermissionYes(List<String> permissions) {
        this.mCallback.onSuccessful();
    }

    @PermissionNo(REQUEST_CODE_SETTING)
    public void getPermissionNo(List<String> permissions) {
       Activity activity= ActivityStackUtil.getInstance().getCurrent();
        // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
        if (AndPermission.hasAlwaysDeniedPermission(activity, permissions)) {
             AndPermission.defaultSettingDialog(activity, REQUEST_CODE_SETTING)
                     .setTitle("权限申请失败")
                     .setMessage("我们需要的一些权限被您拒绝或者系统发生错误申请失败，请您到设置页面手动授权，否则功能无法正常使用！")
                     .setPositiveButton("好，去设置")
                     .show();

            this.mCallback.onFailure();
        }

    }

    public interface PermissionCallback {
        void onSuccessful();

        void onFailure();
    }

}
